
package EditorImagenes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import proyecto1compi1.Colores;
import proyecto1compi1.ColoresEscenario;
import proyecto1compi1.Escenario;
import proyecto1compi1.Interfaz;

/**
 *
 * @authora Mydelin
 */
public class Ventana extends javax.swing.JFrame {
    
   
    JList<JButton> nueva=new JList<JButton>();
 
    int contador;
    JButton actual;
    
    String nombreImagen,extension;
    Mapa map;
 
 
 
    public Ventana(int tamanio,Color color,String nombre,String nombreImagen,String extension) {
        initComponents();
        this.nombreImagen=nombreImagen;
        this.extension=extension;
            map = new Mapa(tamanio,color,nombre,nombreImagen,extension);
            Panel.add(map);
            Panel.setLayout(new BorderLayout(0,0));
            this.setVisible(true);
            
               rellenoColores();
           
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        menu = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Panel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();

        menu.setBackground(new java.awt.Color(0, 0, 0));

        Panel.setPreferredSize(new java.awt.Dimension(540, 374));

        javax.swing.GroupLayout PanelLayout = new javax.swing.GroupLayout(Panel);
        Panel.setLayout(PanelLayout);
        PanelLayout.setHorizontalGroup(
            PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        PanelLayout.setVerticalGroup(
            PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 374, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(Panel);

        javax.swing.GroupLayout menuLayout = new javax.swing.GroupLayout(menu);
        menu.setLayout(menuLayout);
        menuLayout.setHorizontalGroup(
            menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
                .addContainerGap())
        );
        menuLayout.setVerticalGroup(
            menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton1.setText("VER IMAGEN");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jButton2.setText("Aplicar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jLabel1.setText("piintar(");

        jLabel2.setText(");");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/borrador.jpg"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("EDITAR IMAGEN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(64, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(30, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(34, 34, 34))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)))
                .addContainerGap(21, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addContainerGap())
        );

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(169, Short.MAX_VALUE)
                        .addComponent(menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(432, 432, 432)
                        .addComponent(jButton1)
                        .addGap(278, 278, 278)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    public void rellenoColores(){
    
            LinkedList<JButton> colors = new LinkedList<JButton>();
            
            nueva.setBounds(20, 50, 100, 400);
            
            int x=0;
            
            
         Color nuevo = null;
         for (ColoresEscenario aux : Interfaz.ListaDeColoresDelEscenario) {
            for (Colores aux2 : aux.ListacoloresEscnario) {
                    
                    JButton but=new JButton(aux2.idColor);
                  
                    
                    
                    if(aux2.colorHex==null){
                    nuevo=new Color(Integer.valueOf(aux2.rojo),Integer.valueOf(aux2.verde),Integer.valueOf(aux2.azul));
                    
                    but.setBackground(nuevo);
                    but.setBounds(15, x, 70, 50);
                    but.setVisible(true);
                    colors.add(but);
                    x+=50;
                    
                    break;
                    }else{
                    
                    nuevo = Color.decode("#"+aux2.colorHex);
                    but.setBackground(nuevo);
                    but.setBounds(15, x, 70, 50);
                    but.setVisible(true);
                    colors.add(but);
                    x+=50;
                    
                    }
                    
            }
            
           
        }

            
          for(JButton aux: colors){
            nueva.add(aux);
            
            }
          
            
            this.setVisible(true);
            this.add(nueva);
    
    }
    
    
    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
System.out.println(this.nombreImagen+"."+this.extension+"   eeeeeeeeeeeeeeeeeeeeeee  ");

    map.guardarImgane(this.nombreImagen,this.extension);


    
        try {
           
            String imagen=this.nombreImagen+"."+this.extension;
            //String [] img = imagen.split("");
            File dd = new File(imagen);
            Desktop d = Desktop.getDesktop();
            d.open(dd);
        } catch (IOException ex) {
            System.out.println("el maldito error");
        }
        
        
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
   
    String inst =this.jTextField1.getText();
   String intru[] = inst.split(",");
   
   String color = intru[0];
   int x = Integer.parseInt(intru[1].toString());
   int y = Integer.parseInt(intru[2].toString());
    
   
  this.map.Cambiar(x, y, color);
  
  String mostrar="pintar("+color+","+x+","+y+");";
  this.jTextArea1.setText(this.jTextArea1.getText()+"\n"+mostrar);
  
}//GEN-LAST:event_jButton2ActionPerformed



public void evento(java.awt.event.MouseEvent evt,JButton labelseleccionada){

    int posx = evt.getXOnScreen() - this.getX();
        int posy = evt.getYOnScreen() - this.getY();
        labelseleccionada.setLocation((posx-50), (posy-95));
         System.out.println(labelseleccionada);
}



private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
           
    
//    Color nuevo;
//    
//    
//    Point p = MouseInfo.getPointerInfo().getLocation();
//    System.out.println("x: "+p.x+" | y: "+p.y);
//
//    
        System.out.println("------BORRADOR--------");
//     	for(Escenario aux:Interfaz.listaEscenarios ){
//		
//            
//              nuevo=new Color(Integer.valueOf(aux.rojo),Integer.valueOf(aux.verde),Integer.valueOf(aux.azul));
//              nuevo = Color.decode("#"+aux.colorHex); 
//              String a = String.valueOf(nuevo);
//              
//              this.map.Cambiar(p.x, p.y, a);
//            
// 	}
    
    LinkedList<JButton> lista = new LinkedList<JButton>();
  
    contador=contador+1;
    
   //jTextField1.setText(""+contador);

   JButton nuevo = new JButton();
   lista.add(nuevo);
   
   nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/borrador.jpg")));
   this.Panel.add(nuevo);
   nuevo.setBounds(-100,-100 , nuevo.getIcon().getIconWidth()+12, nuevo.getIcon().getIconHeight());
   this.repaint();
  // nuevo.setText(""+contador);
  
   actual=nuevo;
  

   final JButton aux = nuevo;
        nuevo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
           @Override
             public void mouseDragged(java.awt.event.MouseEvent evt) {
                evento(evt,aux);
            }
        });
    
    
    
 
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Panel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel menu;
    // End of variables declaration//GEN-END:variables
}
